import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  Query,
  ALL,
  Put,
  Param,
  Del,
} from '@midwayjs/decorator';
import { ListQuery, RoomManageDTO } from '../dto/roomManage';
import { RoomManageService } from '../service/roomManage';

@Provide()
@Controller('/roomManage')
export class RoomManageController {
  @Inject()
  roomManageService: RoomManageService;

  @Post('/', { description: '新建' })
  async create(@Body(ALL) data: RoomManageDTO) {
    console.log(222, data, data.toEntity())
    return await this.roomManageService.create(data.toEntity());
  }

  @Put('/', { description: '编辑' })
  async edit(@Body(ALL) data: RoomManageDTO) {
    const roomManage = await this.roomManageService.getById(data.id);
    // update
    if (!roomManage) {
      throw new Error('房间不存在');
    }

    const inputData = data.toEntity();
    const ouputData = { ...roomManage, ...inputData };
    ouputData.update_date = new Date();
    return await this.roomManageService.edit(ouputData);
  }

  @Put("/:id", { description: "修改房间状态" })
  async editRoomStatus(@Param('id') id: string, @Body(ALL) data: { status: string }) {
    const roomManage = await this.roomManageService.getById(id);
    if (!roomManage) {
      throw new Error('房间不存在');
    }
    const ouputData = { ...roomManage, status: data.status };
    return await this.roomManageService.edit(ouputData);
  }

  @Del('/:id', { description: '删除' })
  async remove(@Param('id') id: string) {
    const roomManage = await this.roomManageService.getById(id);
    await this.roomManageService.remove(roomManage);
  }

  @Get('/:id', { description: '根据id查询' })
  async getById(@Param('id') id: string) {
    return await this.roomManageService.getById(id);
  }

  @Get('/page', { description: '分页查询' })
  async page(@Query('page') page: number, @Query('size') size: number) {
    return await this.roomManageService.page(page, size);
  }

  @Get('/list', { description: '查询全部' })
  async list(@Query('status') status: string, @Query('type') type: string) {
    let query: Record<string, any> = new ListQuery();
    if (status) {
      query.status = status;
    }
    if (type) {
      query.type = type;
    }
    return await this.roomManageService.getRoomList(query);
  }
}